package free;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadPool {
    public static void main(String[] args){
        int corePoolSize=2;
        int maximunPoolSize=3;
        int keepAliveTime=60;
        TimeUnit unit = TimeUnit.SECONDS;
        BlockingDeque<Runnable> workQueue =
                (BlockingDeque<Runnable>) new ArrayBlockingQueue(1);
        ThreadFactory threadFactory =
                new ThreadFactory() {
                private String prefix = "threadxxx-";
                private AtomicInteger atomicInteger =
                        new AtomicInteger(0);
                    @Override
                    public Thread newThread(Runnable r) {
                        return new Thread(prefix+atomicInteger
                        .incrementAndGet());
                    }
                };
        RejectedExecutionHandler handler =
                new ThreadPoolExecutor.CallerRunsPolicy();
        ThreadPoolExecutor threadPool =
                new ThreadPoolExecutor(
                        corePoolSize,
                        maximunPoolSize,
                        keepAliveTime,
                        unit,
                        workQueue,
                        threadFactory,
                        handler);
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                String tName = Thread.currentThread().getName();
                System.out.println(tName);
                try{Thread.sleep(5000);}catch (Exception e){}
            }
        });
    }
}
